<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    body {
      height: 4000px;
      background: purple;
    }

    #showScroll {
      font-size: 30px;
      position: fixed;
      top: 0;
    }

    #triangle {
      width: 0;
      height: 0;
      /* background: #f00; */
      border: 20px solid transparent;
      position: fixed;
      top: 20px;
      right: 20px;
      border-bottom-color: #000;
    }
  </style>
</head>

<body>
  <p id="showScroll"></p>
  <div id="box">
    <div>111</div>
    <div>222</div>
    <div>111</div>
    <div>222</div>
    <div>111</div>
    <div>222</div>
    <div>111</div>
    <div>222</div>
    <div>111</div>
    <div>222</div>
    <div>111</div>
    <div>222</div>
    <div>111</div>
    <div>222</div>
    <div>111</div>
    <div>222</div>
    <div>111</div>
    <div>222</div>
    <div>111</div>
    <div>222</div>
    <div>111</div>
    <div>222</div>
    <div>111</div>
    <div>222</div>
    <div>111</div>
    <div>222</div>
    <div>111</div>
    <div>222</div>
  </div>
  <div id="triangle"></div>
  <script>
    const showScroll = document.querySelector('#showScroll');
    const box = document.querySelector('#box');
    const html = document.documentElement
    window.addEventListener("scroll", function () {
      showScroll.innerHTML = document.documentElement.scrollTop + 'px';
      if (box.getBoundingClientRect().bottom - html.clientHeight < 30) {
        const div = document.createElement("div");
        div.innerHTML = new Date();
        box.appendChild(div);
      }
    })
    const triangle = document.getElementById('triangle');
    triangle.onclick = function () {
      html.scrollTo({
        top: 0,
        behavior: "smooth"
      })
    }
  </script>
</body>

</html>